<?php

namespace App\Game;

use App\Game\Game;
use App\Contract\PlatInterface;
use App\Game\Traits\Cq9Trait;
use App\Models\GameList;
use Carbon\Carbon;
use Ramsey\Uuid\Uuid;

class cq9 extends Game
{
    use Cq9Trait;

    public $timezone = 'GMT-4';
    private $platName = 'cq9';


    const API_PLAYER_CREATE = '/gameboy/player';
    const API_PLAYER_LOGIN = '/gameboy/player/login';
    const API_JOIN_GAME = '/gameboy/player/gamelink';
    const API_PLAYER_BALANCE = '/gameboy/player/balance';
    const API_TRANSFER_IN = '/gameboy/player/deposit';
    const API_TRANSFER_OUT = '/gameboy/player/withdraw';
    const API_PLAYER_KICK = '/gameboy/player/logout';
    const API_ORDER_LIST = '/gameboy/order/view';
    const API_GAME_LIST = '/gameboy/game/list/';


    public function __construct()
    {
        $this->adds = [
            'main_game_plat_code' => $this->platName,
            'status' => 1
        ];
    }

    /**
     * gameapi
     * 2018/9/11 10:13
     * Administrator
     * getLength
     *
     * @return mixed
     */
    public function getLength()
    {
        return $this->length;
    }

    /**
     * gameapi
     * 2018/9/11 0:08
     * Administrator
     * getPre
     * 获取前缀
     * @return mixed
     */
    public function getPre()
    {
        return $this->pre;
    }


    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }

    /**
     * gameapi
     * 2018/9/16 19:38
     * Administrator
     * getLoginToken
     * 获取玩家登录token
     * @param $playerName
     * @param $password
     * @return null
     */
    public function getLoginToken($playerName, $password)
    {
        $url = $this->gameUrl . self::API_PLAYER_LOGIN;
        $param = [
            'account' => $playerName,
            'password' => $password
        ];
        $result = $this->cq9Request('POST', $url, $param);

        if (array_key_exists('status', $result)) {
            if ($result['status']['code'] == "0") {
                return $result['data']['usertoken'];
            }

        }

        return null;
    }


    /**
     * gameapi
     * 2018/9/11 0:08
     * Administrator
     * curlCreateMember
     * 创建账号
     * @param $accountUserName
     * @param $password
     * @param $request
     * @return array|mixed
     */
    public function curlCreateMember($accountUserName, $password, $request)
    {
        $url = $this->gameUrl . self::API_PLAYER_CREATE;
        $param = [
            'account' => $accountUserName,
            'password' => $password,
            'nickname' => $accountUserName
        ];
        $result = $this->cq9Request('POST', $url, $param);

        if (array_key_exists('status', $result) && isset($result['status']['code']) && $result['status']['code'] == 0) {
            if ($result['status']['code'] == 0) {
                return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => $result['status']['message']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }

    }

    /**
     * gameapi
     * 2018/9/11 0:08
     * Administrator
     * curlGetBalance
     * 获取余额
     * @param $accountUserName
     * @return array|mixed
     */
    public function curlGetBalance($accountUserName)
    {
        $url = $this->gameUrl . self::API_PLAYER_BALANCE . '/' . $accountUserName;
        $result = $this->cq9Request('GET', $url,[]);

        if (array_key_exists('status', $result) && isset($result['status']['code']) && $result['status']['code'] == 0) {
            if ($result['status']['code'] == 0) {
                return ['success' => true, 'data' => ['balance' => $result['data']['balance'],], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => $result['status']['message']];
            }

        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }

    }

    /**
     * gameapi
     * 2018/9/11 0:09
     * Administrator
     * curlTransferIn
     * 账号转入
     * @param $accountUserName
     * @param $price
     * @param $transferid
     * @return array|mixed
     */
    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $url = $this->gameUrl . self::API_TRANSFER_IN;
        $transferid = Uuid::uuid1()->getHex();
        $param = [
            'account' => $accountUserName,
            'mtcode' => $transferid,
            'amount' => $price,
            'eventTime' => Carbon::now($this->timezone)->format('c')
        ];
        $result = $this->cq9Request('POST', $url, $param);

        if (array_key_exists('status', $result)) {
            if ($result['status']['code'] == "0") {
                return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => $result['status']['message']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }


    }

    /**
     * gameapi
     * 2018/9/11 0:09
     * Administrator
     * curlTransferTo
     * 额度转出
     * @param $accountUserName
     * @param $price
     * @param $transferid
     * @return array|mixed
     */
    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        $url = $this->gameUrl . self::API_TRANSFER_OUT;
        $transferid = Uuid::uuid1()->getHex();
        $param = [
            'account' => $accountUserName,
            'mtcode' => $transferid,
            'amount' => $price,
            'eventTime' => Carbon::now($this->timezone)->format('c')
        ];
        $result = $this->cq9Request('POST', $url, $param);

        if (array_key_exists('status', $result)) {
            if ($result['status']['code'] == "0") {
                return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => $result['status']['message']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    /**
     * gameapi
     * 2018/9/11 0:10
     * Administrator
     * curlCheckTransfer
     * 检查订单状态
     * @param $request
     * @return array|mixed
     */
    public function curlCheckTransfer($request)
    {
        $url = $this->gameUrl . '/gameboy/transaction/record/' . $request->transferid;
        $result = $this->cq9Request('GET', $url);

        if (array_key_exists('status', $result)) {
            if ($result['status']['code'] == "0") {
                if ($result['data']['status'] == 'success') {
                    return ['success' => true, 'data' => ['transfer_state' => 1], 'message' => ''];
                } else {
                    return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转账失败，失败原因：' . $result['status']['message']];
                }
            } else {
                return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '查询失败：' . $result['status']['message']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    public function curlKick($request)
    {
        $url = $this->gameUrl . self::API_PLAYER_KICK;
        $param = [
            'account' => $request->accountUserName
        ];
        $result = $this->cq9Request('POST', $url, $param);

        if (array_key_exists('status', $result)) {
            if ($result['status']['code'] == "0") {
                return ['success' => true, 'data' => [], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => $result['status']['message']];
            }

        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }
    }

    /**
     * gameapi
     * 2018/9/11 0:10
     * Administrator
     * curlJoinGame
     * 游戏登录
     * @param $request
     * @return array|mixed
     */
    public function curlJoinGame($request)
    {
        $token = $this->getLoginToken($request->accountUserName, $request->password);

        if ($token) {
            $url = $this->gameUrl . self::API_JOIN_GAME;
            $param = [
                'usertoken' => $token,
                'gamehall' => 'CQ9',
                'gamecode' => $request->gamecode,
                'gameplat' => 'web',
                'lang' => 'zh-cn'
            ];
            $result = $this->cq9Request('POST', $url, $param);

            if (array_key_exists('status', $result)) {
                if ($result['status']['code'] == 0) {
                    return ['success' => true, 'data' => ['url' => $result['data']['url']], 'message' => ''];
                } else {
                    return ['success' => false, 'data' => [], 'message' => $result['status']['message']];
                }
            } else {
                return ['success' => false, 'data' => [], 'message' => '查询错误平台错误'];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '游戏登录失败，登录token未获取到'];
        }

    }

    /**
     * gameapi
     * 2018/9/11 0:10
     * Administrator
     * curlJoinMobileGame
     * 手机端游戏登录
     * @param $request
     * @return array
     */
    public function curlJoinMobileGame($request)
    {
        $token = $this->getLoginToken($request->accountUserName, $request->password);
        if ($token) {
            $url = $this->gameUrl . self::API_JOIN_GAME;
            $param = [
                'usertoken' => $token,
                'gamehall' => 'CQ9',
                'gamecode' => $request->gamecode,
                'gameplat' => 'mobile',
                'lang' => 'zh-cn'
            ];
            $result = $this->cq9Request('POST', $url, $param);

            if (array_key_exists('status', $result)) {
                if ($result['status']['code'] == 0) {
                    return ['success' => true, 'data' => ['url' => $result['data']['url']], 'message' => ''];
                } else {
                    return ['success' => false, 'data' => [], 'message' => $result['status']['message']];
                }
            } else {
                return ['success' => false, 'data' => [], 'message' => '查询错误平台错误'];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '游戏登录失败，登录token未获取到'];
        }
    }

    /**
     * gameapi
     * 2018/9/21 11:37
     * Administrator
     * curlGetBetRecord
     * 获取记录
     * @param Carbon $startTime
     * @param Carbon $endTime
     * @return bool|mixed
     */
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
        $data = [];
        $page = 1;
        $page_size = 2000;
        $start_time = $startTime->format('c');
        $end_time = $endTime->format('c');
        $url = $this->gameUrl . self::API_ORDER_LIST;
        do {
            $flag = false;
            $params = [
                'starttime' => $start_time,
                'endtime' => $end_time,
                'page' => $page,
                'pagesize' => $page_size
            ];

            $result = $this->cq9Request('GET', $url, $params);

            if (isset($result['status'])) {
                if ($result['status']['code'] === '0') {
                    if (count($result['data']['Data']) >= 1) {
                        $data = array_merge($data, $result['data']['Data']);
                        $flag = true;
                    }
                }
            }

            $page++;
        } while ($flag);

        return $data;
    }

    /**
     * gameapi
     * 2018/11/3 19:46
     * Administrator
     * platGameList
     * 获取平台游戏列表
     * @return array
     */
    public function platGameList()
    {
        $url = $this->gameUrl . self::API_GAME_LIST . '/CQ9';
        $result = $this->cq9Request('GET', $url);

        /*foreach ($result['data'] as $item) {
            $cn_name = array_first($item['nameset'], function ($value) {
                if ($value['lang'] == 'zh-cn') {
                    return $value;
                }

            }
                       )['name'];

            $en_name = array_first($item['nameset'], function ($value) {
                if ($value['lang'] == 'en') {
                    return $value;
                }

            }
                       )['name'];

            GameList::create([
                    'unique_key' => Uuid::uuid4(),
                    'main_game_plat_code' => 'cq9',
                    'cn_name' => $cn_name,
                    'en_name' => $en_name,
                    'pc_game_code' => $item['gamecode'],
                    'mobile_game_code' => $item['gamecode'],
                    'record_match_code' => $item['gamecode'],
                    'type' => 1,
                    'category' => 2,
                    'line_num' => 9,
                    'status' => 1,
                ]
            );
        }*/

        if (array_key_exists('status', $result) && isset($result['status']['code']) && $result['status']['code'] == 0) {
            if ($result['status']['code'] == 0) {
                return ['success' => true, 'data' => ['data' => $result['data'],], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => $result['status']['message']];
            }

        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }
    }
}